package jubaozhu.controller.admin;

import jubaozhu.domain.User;
import jubaozhu.extra.base.AdminBaseController;
import jubaozhu.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import zhibi.commons.exception.MessageException;
import zhibi.frame.context.Constant;
import zhibi.utils.security.SecurityUtil;

@Controller
@RequestMapping("admin")
public class AdminController extends AdminBaseController {
    @Autowired
    private UserMapper userMapper;

    @RequestMapping("index")
    public String index() {
        return "admin/index";
    }

    @RequestMapping("welcome")
    public String welcome() {
        return "admin/welcome";
    }

    @GetMapping("login")
    public String login() {
        return "admin/login";
    }

    @PostMapping("login")
    public String login(String username, String password) {
        User user = new User();
        user.setName(username);
        user.setPassword(SecurityUtil.MD5(password).toUpperCase());
        user = userMapper.selectOne(user);
        if (null == user) throw new MessageException("用户名或密码错误");
        logger.info("【 登 录 】 用户:{}  ", user.getName());
        session.setAttribute(Constant.SESSION.ADMIN, user);
        return redirect("index");
    }


    @RequestMapping("logout")
    public String logout() {
        session.removeAttribute(Constant.SESSION.ADMIN);
        return redirect("index");
    }

}
